package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.mapper.SendMeMapper;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.TravelDiscussMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.service.ITravelDiscussService;
import cn.wolfcode.trip.base.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TravelDiscussServiceImpl implements ITravelDiscussService {

    @Autowired
    private TravelDiscussMapper travelDiscussMapper;

    @Override
    public void insert(Long travelId, Long sourceId, String content, int type) {
        TravelDiscuss travelDiscuss = new TravelDiscuss();
        Travel travel = new Travel();
        travel.setId(travelId);
        User user = new User();
        user.setId(sourceId);
        travelDiscuss.setContent(content);
        travelDiscuss.setSource(user);
        travelDiscuss.setTime(new Date());
        travelDiscuss.setTravel(travel);
        travelDiscuss.setType(type);
        travelDiscussMapper.insert(travelDiscuss);
    }

    @Override
    public List<TravelTalk> queryList(Long travelId) {
        List<TravelTalk> list = new ArrayList<TravelTalk>();
        List<String> strings = new ArrayList<>();

        List<TravelDiscuss> travelDiscusses = travelDiscussMapper.queryList(travelId);
        for (TravelDiscuss travelDiscuss : travelDiscusses){
            TravelTalk travelTalk = new TravelTalk();
            travelTalk.setId(travelDiscuss.getId());
            User sourceUser = travelDiscuss.getSource();
            String headImgUrl = sourceUser.getHeadImgUrl();
            travelTalk.setImg(travelDiscuss.getSource().getHeadImgUrl());
            travelTalk.setReplyName(travelDiscuss.getSource().getNickName());
            travelTalk.setId(travelDiscuss.getId());
            travelTalk.setBeReplyName(travelDiscuss.getTravel().getAuthor().getNickName());
            travelTalk.setContent(travelDiscuss.getContent());
            travelTalk.setTime(travelDiscuss.getTime());
            travelTalk.setAddress(travelDiscuss.getSource().getPlace());
            if (travelTalk != null) {
                TravelTalk talk = new TravelTalk();
                travelTalk.getReplyBody().add(talk);
            }
            list.add(travelTalk);
        }
        return list;
    }

    @Override
    public List<TravelDiscuss> queryListByTargetId(Long targetId, int type) {
        return travelDiscussMapper.queryListByTargetId(targetId, type);
    }

    @Override
    public void changeType(int type) {
        travelDiscussMapper.changeType(type);
    }
}
